package com.fox.domain.model;

import java.util.Objects;

/**
 * 订单明细实体（属于 Order 聚合内部）
 */
public class OrderItem {

    private final Long productId;       // 商品 ID
    private final int quantity;         // 数量
    private final Money unitPrice;      // 商品单价（下单时快照）

    public OrderItem(Long productId, int quantity, Money unitPrice) {
        if (productId == null || quantity <= 0 || unitPrice == null) {
            throw new IllegalArgumentException("订单明细参数不合法");
        }
        this.productId = productId;
        this.quantity = quantity;
        this.unitPrice = unitPrice;
    }

    public Long getProductId() {
        return productId;
    }

    public int getQuantity() {
        return quantity;
    }

    public Money getUnitPrice() {
        return unitPrice;
    }

    /**
     * 计算此行小计
     */
    public Money subtotal() {
        return unitPrice.multiply(java.math.BigDecimal.valueOf(quantity));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof OrderItem)) return false;
        OrderItem item = (OrderItem) o;
        return quantity == item.quantity
            && productId.equals(item.productId)
            && unitPrice.equals(item.unitPrice);
    }

    @Override
    public int hashCode() {
        return Objects.hash(productId, quantity, unitPrice);
    }
}